﻿//////////////////////////////////////////////////////////////////
//
// Messages\PlanetOnline.cs
//
// Copyright (c) 2012 Dan Pike.
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation files
// (the "Software"), to deal in the Software without restriction,
// including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software,
// and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using Interfaces;
using ProtoBuf;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Messages
{
   [ProtoContract]
   public class PlanetOnline : Message<PlanetOnline>, IPlanetOnline
   {
      private string planetName_;
      private bool online_;
      private ILocation startLocation_;
      private ILocation orientation_;

      ////////////////////////////////////////////////////////////////////////
      /// <summary>
      /// 
      /// </summary>
      public PlanetOnline()
         : base(ProtoBufTags.IPlanetOnline)
      {
      }

      ////////////////////////////////////////////////////////////////////////
      /// <summary>
      /// Implementation of IPlanetOnline
      /// </summary>
      [ProtoMember((int)ProtoBufTags.PlanetOnlinePlanetName)]
      public string PlanetName
      {
         get { return planetName_; }
         set { planetName_ = value; }
      }

      ////////////////////////////////////////////////////////////////////////
      /// <summary>
      /// Implementation of IPlanetOnline
      /// </summary>
      [ProtoMember((int)ProtoBufTags.PlanetOnlineOnline)]
      public bool Online
      {
         get { return online_; }
         set { online_ = value; }
      }

      ////////////////////////////////////////////////////////////////////////
      /// <summary>
      /// Implementation of IPlanetOnline
      /// 
      /// Note that we serialize this via a __hack property to avoid adding
      /// the ProtoInclude() nastiness to ILocation
      /// </summary>
      public ILocation StartLocation
      {
         get { return startLocation_; }
         set { startLocation_ = value; }
      }

      ////////////////////////////////////////////////////////////////////////
      /// <summary>
      /// Implementation of IPlanetOnline
      /// 
      /// Note that we serialize this via a __hack property to avoid adding
      /// the ProtoInclude() nastiness to ILocation
      /// </summary>
      public ILocation Orientation
      {
         get { return orientation_; }
         set { orientation_ = value; }
      }

      ////////////////////////////////////////////////////////////////////////
      /// <summary>
      /// ProtoBuf.Net cannot handle interfaces in the way that we want. It would
      /// be necessary to adulterate the Interfaces assembly with protobuf-net and
      /// connect to all of the concrete classes and that would kill the plugin-
      /// architecture
      /// </summary>
      [ProtoMember((int)ProtoBufTags.PlanetOnlineStartLocationAndOrientation)]
      public float[] __hack_for_protobuf
      {
         get
         {
            if ((null != startLocation_) && (null != orientation_))
            {
               return new float[6] {
                  startLocation_.Longitude, startLocation_.Latitude, startLocation_.Altitude,
                  orientation_.Longitude, orientation_.Latitude, orientation_.Altitude
               };
            }
            return null;
         }
         set
         {
            startLocation_ = Program.Loader.Create<ILocation>();
            startLocation_.Longitude = value[0];
            startLocation_.Latitude = value[1];
            startLocation_.Altitude = value[2];

            orientation_ = Program.Loader.Create<ILocation>();
            orientation_.Longitude = value[3];
            orientation_.Latitude = value[4];
            orientation_.Altitude = value[5];
         }
      }
   }
}
